﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieModels.net.live.search.api;

namespace MovieModels
{
    public class BingImageSearchResult
    {
        public string Title { get; set; }
        public string Url { get; set; }
        public string MediaURL { get; set; }
        public string ThumbNailURL { get; set; }


        private BingImageSearchResult()
        {

        }

        //Search for movies
        public static IList<BingImageSearchResult> SearchMoviesByName(string searchName)
        {
            //Use Bing Search API, to search movies
            // Details:http://www.codeproject.com/KB/IP/BingAPI.aspx

            BingService service = new BingService();
            SearchRequest request = new SearchRequest();
            request.Adult = AdultOption.Off;
            request.AppId = "709C92F0759A8CD0346D3DE36D24B2D1B98DAEF4";
            request.Query = string.Format("{0} Movie Poster", searchName);
            request.Sources = new SourceType[] { SourceType.Image };
            SearchResponse response = service.Search(request);
            var results = new List<BingImageSearchResult>();
            if (response.Image.Results!=null)
            {
                results = response.Image.Results.Select(m => BingImageSearchResult.MapToBingSearchResult(m)).Take(10).ToList();
            }
            return results;
        }

        private static BingImageSearchResult MapToBingSearchResult(ImageResult result)
        {
            BingImageSearchResult searchResult = new BingImageSearchResult();
            
            //searchResult.DisplayUrl= result.DisplayUrl;
            
            searchResult.Title= result.Title;
            searchResult.Url = result.Url;
            searchResult.MediaURL = result.MediaUrl;
            searchResult.ThumbNailURL = result.Thumbnail.Url;
            return searchResult;
        }
    }
}
